package org.ihelpuoo.orm.handler.http;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.ihelpuoo.lang.ObjectUtils;
import org.ihelpuoo.orm.IIIManual;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.filter.FieldFilter;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.ReadObjectFieldHandler;

public class HttpReadObjectFieldHandler extends ReadObjectFieldHandler
{

	public HttpReadObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected Object evalSubObject(ObjectRule r, Field f, Object src, Object subObj)
			throws Exception
	{
		IIIManual manual = this.processor.getManual();
		String prefix = r.getAlias(f) + ".";
		ObjectRule subRule = manual.getRule(f.getType());

		return manual.getHttpProcessor().fetch(subRule,src,subObj,null,null,prefix);
	}

	@Override
	protected void processWithoutExceptionHandle(ObjectRule r, Field f, Object src, Object obj,
			FieldFilter actived, FieldFilter locked, String prefix) throws Exception
	{
		Object sub = ObjectUtils.newInstance(f.getType());
		Object v = evalSubObject(r,f,src,sub);
		if (null != v)
		{
			Method setter = ObjectUtils.getSetter(obj.getClass(),f.getName(),f.getType());
			setter.invoke(obj,v);
		}
	}
}
